<?php

namespace app\service;


use app\model\NetworkBussinessTimeModel;
use app\model\NetworksModel;
use app\model\PartnerLogModel;
use app\model\TaskModel;
use app\model\WarehouseModel;
use think\facade\Db;

class TaskService
{

    public static function modify_driver($param)
    {
        return TaskModel::modify_driver($param);
    }

    public static function lists($param)
    {
        return TaskModel::lists($param);
    }


    public static function modifyRemarks($param)
    {
        return TaskModel::modifyRemarks($param);
    }

    public static function modifyTaskStatus($param)
    {
        return TaskModel::modifyTaskStatus($param);
    }

    public static function exportByMonth($param)
    {
        return TaskModel::exportByMonth($param);
    }


    public static function detail($param)
    {
        return TaskModel::detail($param);
    }

    public static function details($param)
    {
        if (isset($param['line_id']) && !empty($param['line_id'])) {
            $where[] = ['line_id', '=', $param['line_id']];
            $where[] = ['task_type', '=', date('H') > 14 ? 2 : 1];
            $where[] = ['date', '=', getTodayDate()];
            $record = Db::table('xl_normal_task')->where($where)->find();
            if (empty($record)) {
                return [];
            }

            $param['task_id'] = $record['id'];
        }

        return TaskModel::details($param);
    }

    public static function add($data, $logParam)
    {
        Db::startTrans();
        $res = NetworksModel::add($data);
        $dataLog = [
            'partner_user_id' => $logParam['uid'],
            'log_type' => 2,
            'request_ip' => $logParam['ip'],
            'add_time' => getNowDateTime(),
            'log_desc' => '添加网点',
            'status' => 1,
            'partner_id' => $data['partner_id']
        ];
        
        if ($res ) {
            Db::commit();
            return 10000;
        } else {
            // 回滚事务
            Db::rollback();
            return 10003;
        }
    }

    public static function edit($data, $logParam, $id)
    {


        Db::startTrans();
        $res = NetworksModel::edit($data, $id);
        $dataLog = [
            'partner_user_id' => $logParam['uid'],
            'log_type' => 2,
            'request_ip' => $logParam['ip'],
            'add_time' => getNowDateTime(),
            'log_desc' => '修改网点信息',
            'status' => 1,
            'partner_id' => $data['partner_id']
        ];
        
        if ($res ) {
            Db::commit();
            return 10000;
        } else {
            // 回滚事务
            Db::rollback();
            return 10003;
        }
    }

    public static function del($data, $logParam, $id)
    {
        Db::startTrans();
        $res = NetworksModel::del($id);
        $dataLog = [
            'partner_user_id' => $logParam['uid'],
            'log_type' => 2,
            'request_ip' => $logParam['ip'],
            'add_time' => getNowDateTime(),
            'log_desc' => '删除网点信息',
            'status' => 1,
            'partner_id' => $data['partner_id']
        ];
        
        if ($res ) {
            Db::commit();
            return 10000;
        } else {
            // 回滚事务
            Db::rollback();
            return 10003;
        }
    }


    public static function distributionList($param)
    {
        return TaskModel::distributionList($param);
    }

    public static function lineList($param)
    {
        return TaskModel::lineList($param);
    }

    public static function mapInfo($param)
    {
        return TaskModel::mapInfo($param);
    }

    public static function historyRouteDetails($param)
    {
        return TaskModel::historyRouteDetails($param);
    }

    public static function getTrack($param)
    {
        return TaskModel::getTrack($param);
    }
}